package cn.edu.suda.catalina.health;


import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.edu.suda.catalina.entity.Diary;
import cn.edu.suda.catalina.util.MSQLiteService;
import cn.edu.suda.catalina.util.MyRecyclerViewAdapter;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AnotherFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AnotherFragment extends Fragment {

    public static final String FRAGMENT_TAG = "another";

    private OnAnotherFragmentInteractionListener mListener;

    private MyRecyclerViewAdapter myRecyclerViewAdapter;
    private List<Diary> mDiaries;

    public AnotherFragment() {
    }

    public static AnotherFragment newInstance() {
        return new AnotherFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_another, container, false);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.another_recyclerView);

        Context context = view.getContext();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        mDiaries = new ArrayList<>();
        myRecyclerViewAdapter = new MyRecyclerViewAdapter(mDiaries, mListener);
        recyclerView.setAdapter(myRecyclerViewAdapter);

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.another_fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DiaryFragment.newInstance().show(getFragmentManager(), "editDiary");
            }
        });

        getmDiaries();

        return view;
    }

    private void getmDiaries() {
        mDiaries.clear();
        mDiaries.addAll(new MSQLiteService(getActivity()).getDiary());
        myRecyclerViewAdapter.notifyDataSetChanged();
    }

    public void DataSetChanged() {
        getmDiaries();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnAnotherFragmentInteractionListener) {
            mListener = (OnAnotherFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnAnotherFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnAnotherFragmentInteractionListener {
        void onAnotherFragmentInteraction(Diary diary);
    }

}
